/**
 * Java 8兼容的ContentProvider访问示例
 * 适用于Android项目
 */
public class ContentProviderAccessExample {
    
    private static final String TAG = "ContentProviderExample";
    
    /**
     * 方法1: 查询所有图片
     */
    public void queryAllImages(Context context) {
        try {
            Uri baseUri = Uri.parse("content://com.example.tupiancs34.provider/images");
            ContentResolver resolver = context.getContentResolver();
            
            Cursor cursor = resolver.query(baseUri, null, null, null, null);
            if (cursor != null) {
                int count = cursor.getCount();
                Log.d(TAG, "发现 " + count + " 张图片");
                
                while (cursor.moveToNext()) {
                    String fileName = cursor.getString(cursor.getColumnIndex("_display_name"));
                    long size = cursor.getLong(cursor.getColumnIndex("_size"));
                    Log.d(TAG, "图片: " + fileName + ", 大小: " + size + " bytes");
                }
                
                cursor.close();
            }
        } catch (Exception e) {
            Log.e(TAG, "查询图片失败", e);
        }
    }
    
    /**
     * 方法2: 加载单张图片到ImageView
     */
    public void loadImageToImageView(Context context, ImageView imageView, String fileName) {
        try {
            Uri imageUri = Uri.parse("content://com.example.tupiancs34.provider/images/" + fileName);
            
            // 方法2a: 直接设置URI（推荐）
            imageView.setImageURI(imageUri);
            
            // 方法2b: 手动加载（如果需要处理）
            // loadImageManually(context, imageView, imageUri);
            
        } catch (Exception e) {
            Log.e(TAG, "加载图片失败", e);
        }
    }
    
    /**
     * 方法3: 手动加载图片（Java 8兼容）
     */
    public void loadImageManually(Context context, ImageView imageView, Uri imageUri) {
        InputStream inputStream = null;
        try {
            ContentResolver resolver = context.getContentResolver();
            inputStream = resolver.openInputStream(imageUri);
            
            if (inputStream != null) {
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                if (bitmap != null) {
                    imageView.setImageBitmap(bitmap);
                    Log.d(TAG, "成功加载图片: " + imageUri.toString());
                } else {
                    Log.e(TAG, "无法解码图片");
                }
            } else {
                Log.e(TAG, "无法打开图片流");
            }
            
        } catch (Exception e) {
            Log.e(TAG, "手动加载图片失败", e);
        } finally {
            // 确保关闭输入流
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    Log.e(TAG, "关闭输入流失败", e);
                }
            }
        }
    }
    
    /**
     * 方法4: 获取图片信息
     */
    public void getImageInfo(Context context, String fileName) {
        try {
            Uri imageUri = Uri.parse("content://com.example.tupiancs34.provider/images/" + fileName);
            ContentResolver resolver = context.getContentResolver();
            
            // 获取MIME类型
            String mimeType = resolver.getType(imageUri);
            Log.d(TAG, "图片MIME类型: " + mimeType);
            
            // 获取文件大小
            InputStream inputStream = null;
            try {
                inputStream = resolver.openInputStream(imageUri);
                if (inputStream != null) {
                    int available = inputStream.available();
                    Log.d(TAG, "图片大小: " + available + " bytes");
                }
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (Exception e) {
                        Log.e(TAG, "关闭输入流失败", e);
                    }
                }
            }
            
        } catch (Exception e) {
            Log.e(TAG, "获取图片信息失败", e);
        }
    }
    
    /**
     * 方法5: 分享图片
     */
    public void shareImage(Context context, String fileName) {
        try {
            Uri imageUri = Uri.parse("content://com.example.tupiancs34.provider/images/" + fileName);
            
            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setType("image/*");
            shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            
            Intent chooser = Intent.createChooser(shareIntent, "分享图片");
            context.startActivity(chooser);
            
        } catch (Exception e) {
            Log.e(TAG, "分享图片失败", e);
        }
    }
    
    /**
     * 方法6: 批量处理图片
     */
    public void processAllImages(Context context) {
        try {
            Uri baseUri = Uri.parse("content://com.example.tupiancs34.provider/images");
            ContentResolver resolver = context.getContentResolver();
            
            Cursor cursor = resolver.query(baseUri, null, null, null, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    String fileName = cursor.getString(cursor.getColumnIndex("_display_name"));
                    Uri imageUri = Uri.parse("content://com.example.tupiancs34.provider/images/" + fileName);
                    
                    // 处理每张图片
                    processSingleImage(context, imageUri, fileName);
                }
                cursor.close();
            }
        } catch (Exception e) {
            Log.e(TAG, "批量处理图片失败", e);
        }
    }
    
    private void processSingleImage(Context context, Uri imageUri, String fileName) {
        InputStream inputStream = null;
        try {
            ContentResolver resolver = context.getContentResolver();
            inputStream = resolver.openInputStream(imageUri);
            
            if (inputStream != null) {
                // 这里可以添加图片处理逻辑
                Log.d(TAG, "处理图片: " + fileName);
                
                // 例如：获取图片尺寸
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(inputStream, null, options);
                
                int width = options.outWidth;
                int height = options.outHeight;
                Log.d(TAG, "图片尺寸: " + width + "x" + height);
            }
            
        } catch (Exception e) {
            Log.e(TAG, "处理图片失败: " + fileName, e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    Log.e(TAG, "关闭输入流失败", e);
                }
            }
        }
    }
}
